# -*- coding = utf-8 -*-
# @Time : 2022/3/9 18:19
# @Author : GHHHHHHHHH
# @File : model.py
# @Software : PyCharm

# -*- coding = utf-8 -*-
# @Time : 2022/3/5 0:50
# @Author : GHHHHHHHHH
# @File : bias-funksvd.py
# @Software : PyCharm
import sys

import torch


# 模型设置
class Model(torch.nn.Module):
    def __init__(self, user, item, hidden):
        super(Model, self).__init__()
        # (x, y) -> (x, k) * (k * y)
        self.user_hidden = torch.nn.Parameter(torch.randn((user, hidden), requires_grad=True))
        self.hidden_item = torch.nn.Parameter(torch.randn((hidden, item), requires_grad=True))
        self.user_bias = torch.nn.Parameter(torch.randn(user), requires_grad=True)
        self.item_bias = torch.nn.Parameter(torch.randn(item), requires_grad=True)
        self.register_parameter("user_hidden", self.user_hidden)
        self.register_parameter("hidden_item", self.hidden_item)

    def forward(self):
        return torch.mm(self.user_hidden, self.hidden_item)


# 加入正则化的MSELoss，超参数gamma
class MseLossPro(torch.nn.Module):
    def __init__(self, gamma=0.0075):
        super().__init__()
        self.gamma = gamma

    def forward(self, y_pred, y_true, user_hidden, hidden_item):
        mse = torch.nn.MSELoss()(y_pred, y_true)
        loss = mse + self.gamma * (torch.norm(user_hidden) ** 2 + torch.norm(hidden_item) ** 2)
        return loss









